var JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/nodeData';
const ObjectId = require('mongodb').ObjectId
var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = 'secret';



module.exports = passport =>{
    passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
        MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {
            if (err) throw err;
            var dbo = db.db("nodeData");
            dbo.collection("site").findOne({"_id":ObjectId(jwt_payload.id)}, function (err, user) {
                if(user){
                    return done(null,user);
                }
                return done(null,false);
            });
        });

    }));
}